<?php

namespace app\common\model\Product;

use app\common\model\Order\Product as OrderProduct;
use think\Model;
use traits\model\SoftDelete;


class Product extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'product';
    // 忽略数据表不存在的字段
    protected $field = true;
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    //禁止写入时间字段
    protected $updateTime = false;
    // 软删除的字段
    protected $deleteTime = 'deletetime';
    // 设置返回的结果类型
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [
        'thumbs_text',
        'type_text',
        'unit_text',
        'rate',
        'flag_text',
        'stock_status',
        'status_text',
    ];
    public function type()
    {
        return $this->belongsTo(Type::class,'typeid','id',[],'left')->setEagerlyType(0);
    }
    public function unit()
    {
        return $this->belongsTo(Unit::class,'unitid','id',[],'left')->setEagerlyType(0);
    }
    public function getThumbsTextAttr($value,$data)
    {
        $defaultImg = config('site.productImg')??'/uploads/productImg.jpg';
        return is_file(ROOT_PATH.'public'.$data['thumbs'])?$data['thumbs']:$defaultImg;
    }
    public function getTypeTextAttr($value,$data)
    {
        $type = $this->hasOne(Type::class,'id','typeid')->find();
        return $type?$type['name']:'';
    }
    public function getUnitTextAttr($value,$data)
    {
        $unit = $this->hasOne('unit','id','unitid')->find();
        return $unit?$unit['name']:'个';
    }
    public function getRateAttr()
    {
        $where = ['status'=>'3'];
        $count = $this->hasMany(OrderProduct::class,'proid','id',[],'left')->where($where)->count();
        $rate = $this->hasMany(OrderProduct::class,'proid','id',[],'left')->where($where)->avg('rate');
        return $count==0?-1:round($rate,1);
    }
    // public function getFlagTextAttr($value,$data)
    // {
    //     $flag = $data['flag']??-1;
    //     switch($flag){
    //         case -1:
    //             return '';
    //         case 1:
    //             return '新品';
    //         case 2:
    //             return '热销';
    //         case 3:
    //             return '推荐';
    //     }
    // }
    public function getStockStatusAttr($value,$data)
    {
        $stock = $data['stock']??0;
        switch($stock){
            case 0:
                return '0';
            case $stock>100:
                return '1';
            case $stock<=100:
                return '2';
        }
    }
    public function getFlagList()
    {
        return ['1' => __('new-product'), '2' => __('hot-sale'), '3' => __('recommend')];
    }

    public function getStatusList()
    {
        return ['0' => __('sold out'), '1' => __('putaway')];
    }


    public function getFlagTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
        $list = $this->getFlagList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
}
